package com.jn.okhttp.views;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.jn.okhttp.R;
import com.jn.okhttp.base.BaseActivity;
import com.jn.okhttp.contract.ZidianContract;
import com.jn.okhttp.entity.ZidianEntity;
import com.jn.okhttp.presenter.ZidianPresenter;

public class ZidianActivity extends BaseActivity implements ZidianContract.IZidianView {

    private EditText etQueryText;
    private Button btnSearch;
    private ProgressBar pbLoading;
    private TextView tvHanzi, tvPinyin, tvBihua, tvBushou, tvWords, tvBasicExplain, tvDetailExplain;
    private TextView tvErrorMessage;

    private ZidianContract.IZidianPresenter presenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_zidian;
    }

    @Override
    public void init() {
        etQueryText = findViewById(R.id.et_query_text);
        btnSearch = findViewById(R.id.btn_search);
        pbLoading = findViewById(R.id.pb_loading);
        tvHanzi = findViewById(R.id.tv_hanzi);
        tvPinyin = findViewById(R.id.tv_pinyin);
        tvBihua = findViewById(R.id.tv_bihua);
        tvBushou = findViewById(R.id.tv_bushou);
        tvWords = findViewById(R.id.tv_words);
        tvBasicExplain = findViewById(R.id.tv_basic_explain);
        tvDetailExplain = findViewById(R.id.tv_detail_explain);
        tvErrorMessage = findViewById(R.id.tv_error_message);
        presenter = new ZidianPresenter(this);
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String text = etQueryText.getText().toString().trim();
                if (text.isEmpty()) {
                    Toast.makeText(ZidianActivity.this, "请输入查询内容", Toast.LENGTH_SHORT).show();
                } else {
                    presenter.getData(text);
                }
            }
        });
    }

    @Override
    public void onHttpLoading(boolean loading) {
        pbLoading.setVisibility(loading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onHttpFail(String... msg) {
        Toast.makeText(this, "请求失败: " + msg[0], Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onHttpTimeout() {
        Toast.makeText(this, "请求超时", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onHttpTokenExpiration() {
        Toast.makeText(this, "令牌过期", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showData(ZidianEntity zIdianEntity) {
        if (zIdianEntity == null || zIdianEntity.getResult() == null) {
            showError("没有查询到相关数据");
            return;
        }

        ZidianEntity.Result result = zIdianEntity.getResult();

        // 添加日志输出
        Log.d("ZidianActivity", "Hanzi: " + result.getHanzi());
        Log.d("ZidianActivity", "Pinyin: " + result.getPinyin());
        Log.d("ZidianActivity", "Bihua: " + result.getBihua());
        Log.d("ZidianActivity", "Bushou: " + result.getBushou());
        Log.d("ZidianActivity", "Words: " + result.getWords());

        tvHanzi.setText(result.getHanzi());
        tvPinyin.setText(result.getPinyin());
        tvBihua.setText(result.getBihua());
        tvBushou.setText(result.getBushou());
        tvWords.setText(result.getWords());

        StringBuilder basicExplainBuilder = new StringBuilder();
        for (String explain : result.getBasic_explain()) {
            basicExplainBuilder.append(explain).append("\n");
        }
        tvBasicExplain.setText(basicExplainBuilder.toString());

        StringBuilder detailExplainBuilder = new StringBuilder();
        for (String explain : result.getDetail_explain()) {
            detailExplainBuilder.append(explain).append("\n");
        }
        tvDetailExplain.setText(detailExplainBuilder.toString());
    }

    @Override
    public void showError(String error) {
        tvErrorMessage.setVisibility(View.VISIBLE);
        tvErrorMessage.setText(error);
    }
}